//
//  WJDrawDownView.m
//  weibo
//
//  Created by 王俊 on 15/12/27.
//  Copyright © 2015年 王俊. All rights reserved.
//

#import "WJDrawDownView.h"
@interface WJDrawDownView()
@property(nonatomic,strong) UIImageView  *containerView;
@end

@implementation WJDrawDownView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //建议在这里做View的初始化
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.containerView];
        
    }
    return self;
}
- (UIImageView*)containerView
{
    if (!_containerView) {
        UIImageView *containerView = [[UIImageView alloc]init];
        containerView.image = [UIImage imageNamed:@"popover_background"];
        containerView.height = 44*3;
        containerView.width = 120;
        _containerView = containerView;
        _containerView.userInteractionEnabled = true;
    }
    return _containerView;
}

- (void)setContentView:(UIView *)contentView
{
    _contentView = contentView;
    _contentView.x = 10;
    _contentView.y = 15;
    self.containerView.height = CGRectGetMaxY(contentView.frame) + 15;
    self.containerView.width = CGRectGetMaxX(contentView.frame) + 10;
    [self.containerView addSubview:_contentView];
}

+ (instancetype)drawView
{
    return [[self alloc]init];
}

- (void)show:(UIView*)view
{
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    self.frame = window.frame;
    CGRect newFrame = [view convertRect:view.bounds toView:nil];
    self.containerView.centerX = CGRectGetMidX(newFrame);
    self.containerView.y = CGRectGetMaxY(newFrame);
    [window addSubview:self];
}
- (void)dismiss
{
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(WJDrawDownViewDismiss:)]) {
        [self.delegate WJDrawDownViewDismiss:self];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}

@end
